#!/bin/sh
#
# This is the script for generating files for a specific Dalton test job.
#
# For the .check file ksh or bash is preferred, otherwise use sh
# (and hope it is not the old Bourne shell, which will not work)
#
if [ -x /bin/ksh ]; then
   CHECK_SHELL='#!/bin/ksh'
elif [ -x /bin/bash ]; then
   CHECK_SHELL='#!/bin/bash'
else
   CHECK_SHELL='#!/bin/sh'
fi


#######################################################################
#  TEST DESCRIPTION
#######################################################################
cat > cc_geoopt.info <<'%EOF%'
   cc_geoopt
   ---------
   Molecule:         N2H2 in C1
   Wave Function:    CCSD / cc-pVDZ
   Test Purpose:     Geometry optimization using analytic CCSD gradient
%EOF%

#######################################################################
#  INTEGRAL INPUT
#######################################################################
cat > cc_geoopt.mol <<'%EOF%'
BASIS
cc-pVDZ
  Cis-diazine CCSD geometry optimisation (cc-dzp basis)
  001  ( A1)
    2    2  Y  X      1.00D-15
        7.0   1    
N    1.1798400 0.0000000 -0.12616200       *
        1.0   1    
H    1.9234220 0.0000000 1.75294370       *
%EOF%

#######################################################################
#  DALTON INPUT
#######################################################################
cat > cc_geoopt.dal <<'%EOF%'
**DALTON INPUT
.OPTIMIZE
**INTEGRAL
.DIPLEN
.DEROVL
.DERHAM
**WAVE FUNCTIONS
.CC
*SCF INPUT
.THRESH
 1.0D-10
*CC INPUT
.CCSD
.THRLEQ
 1.0D-08
.THRENR
 1.0D-10
**END OF DALTON INPUT
%EOF%
#######################################################################

 
#######################################################################
#  CHECK SCRIPT
#######################################################################
echo $CHECK_SHELL >cc_geoopt.check
cat >>cc_geoopt.check <<'%EOF%'
log=$1

if [ `uname` = Linux ]; then
   GREP="egrep -a"
else
   GREP="egrep"
fi

if $GREP -q "not implemented for parallel calculations" $log; then
   echo "TEST ENDED AS EXPECTED"
   exit 0
fi

# initial SCF, MP2 and CCSD energies: 
CRIT1=`$GREP "Final * HF energy: * \-109\.99363488" $log | wc -l`
CRIT2=`$GREP "Total MP2   energy: * \-110\.33256868" $log | wc -l`
CRIT3=`$GREP "Total CCSD  energy: * \-110\.35074180" $log | wc -l`
TEST[1]=`expr $CRIT1 \+ $CRIT2 \+ $CRIT3` 
CTRL[1]=7
ERROR[1]="SCF, MP2 OR CCSD ENERGIES AT THE START GEOMETRY ARE NOT CORRECT"

# initial CCSD (frozen core) total molecular gradient: 
CRIT1=`$GREP "N *_1 *( \-|\-0)\.00597115.. * (\-0|\-| |0)\.0000000000 * ( \-|\-0)\.01404906.." $log | wc -l`
CRIT2=`$GREP "N *_2 *( |0)\.00597115.. * (\-0|\-| |0)\.0000000000 * ( \-|\-0)\.01404906.." $log | wc -l`
CRIT3=`$GREP "H *_1 * ( |0)\.00473117.. * (\-0|\-| |0)\.0000000000 * ( |0)\.01404906.." $log | wc -l`
CRIT4=`$GREP "H *_2 * ( \-|\-0)\.00473117.. * (\-0|\-| |0)\.0000000000 * ( |0)\.01404906.." $log | wc -l`
TEST[2]=`expr $CRIT1 \+ $CRIT2 \+ $CRIT3 \+ $CRIT4` 
CTRL[2]=4
ERROR[2]="THE TOTAL MOLECULAR GRADIENT AT THE START GEOMETRY IS NOT CORRECT"

# final geometry:
CRIT1=`$GREP "N *_1 * 1\.1813598... * (\-0|\-| |0)\.0000000000 * ( \-|\-0)\.105433...." $log | wc -l`
CRIT2=`$GREP "N *_2 * \-1\.1813598... * (\-0|\-| |0)\.0000000000 * ( \-|\-0)\.105433...." $log | wc -l`
CRIT3=`$GREP "H *_1 * 1\.912616.... * (\-0|\-| |0)\.0000000000 * 1\.732215...." $log | wc -l`
CRIT4=`$GREP "H *_2 * \-1\.912616.... * (\-0|\-| |0)\.0000000000 * 1\.732215...." $log | wc -l`
TEST[3]=`expr $CRIT1 \+ $CRIT2 \+ $CRIT3 \+ $CRIT4` 
CTRL[3]=16
ERROR[3]="THE FINAL GEOMETRY IS NOT CORRECT"

# summary of the geometry optimization:
CRIT1=`$GREP "Geometry converged in * 5 *iterations!" $log | wc -l`
CRIT2=`$GREP "Energy at final geometry is * : * \-110\.351402 a.u." $log | wc -l`
TEST[4]=`expr $CRIT1 \+ $CRIT2` 
CTRL[4]=2
ERROR[4]="SUMMARY OF THE OPTIMIZATION IS NOT CORRECT"


PASSED=1
for i in 1 2 3 4
do 
   if [ ${TEST[i]} -ne ${CTRL[i]} ]; then
     echo "${ERROR[i]} ( test = ${TEST[i]}; control = ${CTRL[i]} ); "
     PASSED=0
   fi
done 

if [ $PASSED -eq 1 ]
then
  echo TEST ENDED PROPERLY
  exit 0
else
  echo THERE IS A PROBLEM 
  exit 1
fi

%EOF%
chmod +x cc_geoopt.check
#######################################################################
